namespace ETestIT.UI.AppControls
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using SharpArch.Core.DomainModel;

    public abstract class UserControlBase<T> : UserControl where T : Entity
    {
        public event EventHandler Update;

        protected UserControlBase()
        {
            Visible = false;
        }

        public virtual void ShowModel(T entity)
        {
            Visible = true;
        }

        public virtual void UpdateModel(T entity)
        {
            Visible = false;
        }

        protected void bntSave_Clicked(object sender, EventArgs e)
        {
            if (Update != null)
                Update(sender, e);
        }

        protected void BindDdlToIList<EL>(ListControl dropDownList, IList<EL> entitiesList, string idField, string valueField)
        {
            dropDownList.DataSource = entitiesList;
            dropDownList.DataValueField = idField;
            dropDownList.DataTextField = valueField;
            dropDownList.DataBind();
        }

        protected void DindDdlToEnum<ET>(ListControl listControl)
        {
            listControl.DataSource = Enum.GetNames(typeof(ET));
            listControl.DataBind();
        }

        protected ET GetEnumValueFromListControl<ET>(ListControl listControl)
        {
            return (ET)Enum.Parse(typeof(ET) ,listControl.SelectedValue); 
        }
    }
}